package cn.ApplicationComponent;


import cn.CommonUtils.ApplicationConfigurationSingletonFactory;
import cn.Dao.ApplicationConfigurationDao;
import cn.Entity.ApplicationConfiguration;
import cn.Quartz.Job.GenerateReport2Job;
import cn.Quartz.Job.HelloJob;
import cn.Quartz.Scheduler.SchedulerInstance;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;
import java.util.Date;
import java.util.List;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

@Slf4j
@Service
public class WebAPPBootListener implements InitializingBean, ServletContextAware {

    static boolean isStarted = false;

//    @Autowired
//    private ApplicationConfigurationSingletonFactory applicationConfigurationSingletonFactory;

//    @Autowired
//    private ApplicationConfigurationDao applicationConfigurationDao;

    public void setServletContext(ServletContext servletContext) {
            System.out.println("boot setServletContext ..."+new Date().toString());
//
        if(!isStarted){
            isStarted = true;
            /**
            * 程序启动载入配置
            */

            Thread t1 = new Thread(()->{
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                ApplicationConfigurationSingletonFactory.getInstance();


            });


            t1.start();



        JobDetail job = JobBuilder.newJob(HelloJob.class).withIdentity("白金福气袋生成每日报表", "group1").build();
//        CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0 */2 * * * ?"))
//                .build();

            CronTrigger trigger = newTrigger().withIdentity("trigger1", "group1").withSchedule(cronSchedule("0 43 14 * * ?"))
                    .build();

            JobDetail job2 = JobBuilder.newJob(GenerateReport2Job.class).withIdentity("白金福气袋生成每日报表2", "group1").build();
            CronTrigger trigger2 = newTrigger().withIdentity("trigger2", "group1").withSchedule(cronSchedule("0 43 14 * * ?"))
                    .build();

        Date ft = null;
        try {
            ft = SchedulerInstance.getInstance().scheduleJob(job, trigger);
            ft = SchedulerInstance.getInstance().scheduleJob(job2, trigger2);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
//

        try {
            SchedulerInstance.getInstance().start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }


        }
    }

    public void afterPropertiesSet() throws Exception {
        System.out.println("boot afterPropertiesSet ... " + new Date().toString());
    }
}